﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKRevitKernel.FormUtility;

namespace YJKRevitRoom.RoomNameSearchReplace
{
	public partial class RoomNameSearchReplaceForm : System.Windows.Forms.Form
	{
		public RoomNameSearchReplaceForm.SelecionType SeletType
		{
			get
			{
				return this.m_SelectionType;
			}
			set
			{
				this.m_SelectionType = value;
			}
		}

		public RoomNameSearchReplaceForm.RoomTagOperation RoomTagOper
		{
			get
			{
				return this.m_RoomTagOper;
			}
		}

		public bool OperChange
		{
			get
			{
				return this.m_bOperChange;
			}
			set
			{
				this.m_bOperChange = value;
			}
		}

		public string SearchText
		{
			get
			{
				return this.cmbSearch.Text;
			}
		}

		public string ReplaceText
		{
			get
			{
				return this.cmbReplace.Text;
			}
		}

		public string PrefixText
		{
			get
			{
				return this.cmbPrefix.Text;
			}
		}

		public string SuffixText
		{
			get
			{
				return this.cmbSuffix.Text;
			}
		}

		public RoomNameSearchReplaceForm()
		{
			this.InitializeComponent();
			this.m_SelectionType = RoomNameSearchReplaceForm.SelecionType.eCurView;
			this.Text = "查找和替换";
			this.cmbSearch.Text = string.Empty;
			this.toolTip1.SetToolTip(this.btnPickSearch, "拾取查找内容");
			this.toolTip1.SetToolTip(this.btnPickRep, "拾取替换内容");
			this.radReplace.Checked = true;
			this.radReplace_CheckedChanged(this, new EventArgs());
			this.cmbSearch_TextChanged(this, new EventArgs());
			this.m_thisManger = new ControlInfoManager(this, new List<KeyValuePair<System.Windows.Forms.Control, bool>>
			{
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radReplace, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radPreSuffix, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbSearch, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbReplace, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbPrefix, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbSuffix, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radBoxSel, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radCurView, false),
				new KeyValuePair<System.Windows.Forms.Control, bool>(this.radAllView, false)
			}, null, null);
		}

		private void RoomNameSearchReplaceForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (this.m_thisManger != null)
			{
				this.m_thisManger.SetControlInfoToForm();
			}
		}

		private void radReplace_CheckedChanged(object sender, EventArgs e)
		{
			this.lblPrefix.Visible = false;
			this.lblSuffix.Visible = false;
			this.cmbPrefix.Visible = false;
			this.cmbSuffix.Visible = false;
			this.lblReplace.Visible = true;
			this.cmbReplace.Visible = true;
			this.btnPickRep.Visible = true;
			this.Text = "查找和替换";
			this.m_RoomTagOper = RoomNameSearchReplaceForm.RoomTagOperation.eReplace;
		}

		private void radPreSuffix_CheckedChanged(object sender, EventArgs e)
		{
			this.lblPrefix.Visible = true;
			this.lblSuffix.Visible = true;
			this.cmbPrefix.Visible = true;
			this.cmbSuffix.Visible = true;
			this.lblReplace.Visible = false;
			this.cmbReplace.Visible = false;
			this.btnPickRep.Visible = false;
			this.Text = "查找和加前后缀";
			this.m_RoomTagOper = RoomNameSearchReplaceForm.RoomTagOperation.ePreSuffix;
		}

		private void btnPickSearch_Click(object sender, EventArgs e)
		{
			this.m_bOperChange = true;
			this.m_SelectionType = RoomNameSearchReplaceForm.SelecionType.ePickSearch;
			this.ChangeOperateState();
		}

		private void btnPickRep_Click(object sender, EventArgs e)
		{
			this.m_bOperChange = true;
			this.m_SelectionType = RoomNameSearchReplaceForm.SelecionType.ePickReplace;
			this.ChangeOperateState();
		}

		private void radBoxSel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radBoxSel.Checked)
			{
				this.m_bOperChange = true;
				this.btnCheck.Enabled = false;
				this.btnReplace.Enabled = false;
				this.cmbSearch_TextChanged(this, new EventArgs());
				this.m_SelectionType = RoomNameSearchReplaceForm.SelecionType.eBoxSel;
				this.ChangeOperateState();
			}
		}

		private void radCurView_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radCurView.Checked)
			{
				this.m_bOperChange = true;
				this.btnCheck.Enabled = true;
				this.btnReplace.Enabled = true;
				this.cmbSearch_TextChanged(this, new EventArgs());
				this.m_SelectionType = RoomNameSearchReplaceForm.SelecionType.eCurView;
				this.ChangeOperateState();
			}
		}

		private void radAllView_CheckedChanged(object sender, EventArgs e)
		{
			this.m_bOperChange = true;
			this.btnCheck.Enabled = false;
			this.btnReplace.Enabled = true;
			this.m_SelectionType = RoomNameSearchReplaceForm.SelecionType.eAllView;
			this.cmbSearch_TextChanged(this, new EventArgs());
			this.ChangeOperateState();
		}

		private void ChangeOperateState()
		{
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			RoomNameSearchReplaceForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			RoomNameSearchReplaceForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			RoomNameSearchReplaceForm.SendMessage(CmdRoomNameSearchReplace._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void btnCheck_Click(object sender, EventArgs e)
		{
			CmdRoomNameSearchReplace._roomNameMag.CheckInCurView(this.SearchText);
		}

		private void btnReplace_Click(object sender, EventArgs e)
		{
			CmdRoomNameSearchReplace._roomNameMag.ModifyRoomTagName(this);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.RefreshSearchData();
			CmdRoomNameSearchReplace._roomNameMag.RefreshSearchData();
		}

		private void RoomNameSearchReplaceForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.RefreshSearchData();
				CmdRoomNameSearchReplace._roomNameMag.RefreshSearchData();
			}
		}

		private void RoomNameSearchReplaceForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.RefreshSearchData();
			CmdRoomNameSearchReplace._roomNameMag.RefreshSearchData();
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		public void RefreshSearchData()
		{
			this.m_bOperChange = false;
			this.ChangeOperateState();
			this.SimulationPressESC();
		}

		private void cmbSearch_TextChanged(object sender, EventArgs e)
		{
			if (this.cmbSearch.Text == "")
			{
				this.btnReplace.Enabled = false;
				return;
			}
			if (this.radBoxSel.Checked)
			{
				this.btnReplace.Enabled = false;
				return;
			}
			this.btnReplace.Enabled = true;
		}

		private void cmbSearch_Leave(object sender, EventArgs e)
		{
			string text = this.cmbSearch.Text;
			if (text == "")
			{
				this.btnReplace.Enabled = false;
			}
			else
			{
				this.btnReplace.Enabled = true;
				if (!this.cmbSearch.Items.Contains(text))
				{
					this.cmbSearch.Items.Add(text);
				}
			}
			if (this.radBoxSel.Checked)
			{
				this.btnReplace.Enabled = false;
			}
		}

		private void cmbReplace_Leave(object sender, EventArgs e)
		{
			string text = this.cmbReplace.Text;
			if (text != "" && !this.cmbReplace.Items.Contains(text))
			{
				this.cmbReplace.Items.Add(text);
			}
		}

		private void cmbPrefix_Leave(object sender, EventArgs e)
		{
			string text = this.cmbPrefix.Text;
			if (text != "" && !this.cmbPrefix.Items.Contains(text))
			{
				this.cmbPrefix.Items.Add(text);
			}
		}

		private void cmbSuffix_Leave(object sender, EventArgs e)
		{
			string text = this.cmbSuffix.Text;
			if (text != "" && !this.cmbSuffix.Items.Contains(text))
			{
				this.cmbSuffix.Items.Add(text);
			}
		}

		private RoomNameSearchReplaceForm.SelecionType m_SelectionType = RoomNameSearchReplaceForm.SelecionType.eCurView;

		private RoomNameSearchReplaceForm.RoomTagOperation m_RoomTagOper;

		private bool m_bOperChange;

		private ControlInfoManager m_thisManger;

		public enum SelecionType
		{
			eBoxSel,
			eCurView,
			eAllView,
			ePickSearch,
			ePickReplace
		}

		public enum RoomTagOperation
		{
			eReplace,
			ePreSuffix
		}
	}
}
